﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace PizzaLunch
{
    public class SinaSource : TweetSource
    {
        private const string STATUS_FRIENDS_TIMELINE = "statuses/friends_timeline.xml";
        private const string STATUS_UPDATE = "statuses/update.xml";
        private const string STATUS_COMMENTS = "statuses/comments.xml";
        private const string STATUS_RETWEET = "statuses/repost.xml";
        private const string STATUS_COMMENT = "statuses/comment.xml";
        private const string STATUS_FRIENDS = "statuses/friends";
        private const string STATUS_FOLLOWERS = "statuses/followers";
        private const string STATUS_DESTROY = "statuses/destroy";
        private const string STATUS_USER_TIMELINE = "statuses/user_timeline";

        private const string USERS_SHOW = "users/show";

        private const string ACCOUNT_VERIFY = "account/verify_credentials.xml";
        private const string REQUEST_TYPE_XML = ".xml";
        public override void CreateSetting()
        {
            if (string.IsNullOrEmpty(_sourceCfg.customKey))
                _sourceCfg.customKey = "240426366";
            if (string.IsNullOrEmpty(_sourceCfg.customSecrect))
                _sourceCfg.customSecrect = "a58bd5179fc0dcc3a698331ebda51c40";
            if (string.IsNullOrEmpty(_sourceCfg.callbackUrl))
                _sourceCfg.callbackUrl = "oob";
            if (string.IsNullOrEmpty(_sourceCfg.baseUrl))
                _sourceCfg.baseUrl = "http://api.t.sina.com.cn/oauth/";
            if (string.IsNullOrEmpty(_sourceCfg.requestUrl))
                _sourceCfg.requestUrl = "http://api.t.sina.com.cn/";
        }

        public SinaSource()
            : base("Sina")
        {
            ShowName = "Sina";
            Logo = "Resource/Sina16x16.png";
        }

        private static string TransferSex(string sex)
        {

            switch (sex)
            {
                case "m":
                    return "Male";
                case "f":
                    return "Femail";
                default:
                    return "Unknown";
            }
        }

        private static long TransferTweet(XmlNode node, TweetItem ti)
        {
            ti.ID = node["id"].InnerText;
            ti.Content = node["text"].InnerText;
            ti.DateUpdate = Util.ConverDateTime(node["created_at"].InnerText);
            long timestamp = System.Convert.ToInt64(ti.ID);
            if (node["original_pic"] != null)
            {
                ti.Image = node["original_pic"].InnerText;
                if (node["thumbnail_pic"] != null)
                    ti.Thumbnail = node["thumbnail_pic"].InnerText;
                else
                    ti.Thumbnail = ti.Image;
                ti.HasImage = true;
            }
            return timestamp;
        }

        private static void TransferUserNode(XmlNode node, UserInfo user)
        {
            if (node == null || user == null)
                return;
            user.ID = node["id"].InnerText;
            user.ShowName = node["screen_name"].InnerText;
            user.HeadUrl = node["profile_image_url"].InnerText;
            if (string.IsNullOrEmpty(user.HeadUrl))
            {
                user.HeadUrl = "Resource/NoHead.jpg";
            }
            if (node["verified"].InnerText == "true")
                user.VIP = true;
            else
                user.VIP = false;
            user.Gender = TransferSex(node["gender"].InnerText);
            user.Location = node["location"].InnerText;
            user.Signature = node["description"].InnerText;
            user.friends = Convert.ToInt64(node["friends_count"].InnerText);
            user.follows = Convert.ToInt64(node["followers_count"].InnerText);
            user.topics = Convert.ToInt64(node["statuses_count"].InnerText);
            user.Updated = true;
        }

        public override string GetItemUrl(TweetItem ti)
        {
            //http://api.t.sina.com.cn/:userid/statuses/:id.(json|xml)
            string url = SourceCfg.requestUrl + ti.Author.GetID(ti.Account) + "/statuses/" + ti.ID;
            return url;
        }

        public override bool Query(TweetAccount tac)
        {
            List<Parameter> parameters = new List<Parameter>();
            oAuth.OauthKey oauthKey = new oAuth.OauthKey();
            oauthKey.customKey = SourceCfg.customKey;
            oauthKey.customSecrect = SourceCfg.customSecrect;
            oauthKey.tokenKey = tac.AccountCfg.accessKey;
            oauthKey.tokenSecrect = tac.AccountCfg.accessSecrect;

            if (tac.LastTimeStamp != 0)
                parameters.Add(new Parameter("since_id", tac.LastTimeStamp.ToString()));
            parameters.Add(new Parameter("count", MainControl.GlobalSetting.REFRESH_ITEM_COUNT.ToString()));

            string queryString, retString;
            try
            {
                HttpRequest request = new HttpRequest();
                oAuth.GetOauthUrl(SourceCfg.requestUrl + STATUS_FRIENDS_TIMELINE, HttpRequest.HTTP_GET, oauthKey, parameters, out queryString);
                retString = request.HttpGet(SourceCfg.requestUrl + STATUS_FRIENDS_TIMELINE, queryString);
                if (string.IsNullOrEmpty(retString))
                    return false;
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(retString);
                XmlNodeList nodeList = doc.SelectNodes("/statuses/status");
                if (nodeList != null)
                {
                    foreach (XmlNode node in nodeList)
                    {
                        TweetItem ti = new TweetItem(tac, node.InnerXml);
                        UserInfo author = new UserInfo(tac);

                        ti.Type = TweetItem.TweetItemType.SOURCE;
                        long timestamp = TransferTweet(node, ti);
                        if (timestamp > tac.LastTimeStamp)
                            tac.LastTimeStamp = timestamp;

                        if (node["user"] != null)
                        {
                            TransferUserNode(node["user"], author);
                            MainControl.UpdateAllUser(author, ti);
                        }
                        if (node["retweeted_status"] != null)
                        {
                            ti.SourceItem = new TweetItem(tac, node["retweeted_status"].InnerXml);
                            TransferTweet(node["retweeted_status"], ti.SourceItem);

                            if (node["retweeted_status"]["user"] != null)
                            {
                                UserInfo oriAuthor = new UserInfo(tac);
                                TransferUserNode(node["retweeted_status"]["user"], oriAuthor);
                                MainControl.UpdateAllUser(oriAuthor, ti.SourceItem);
                            }
                        }
                        MainControl.UpdateTweets(ti);
                    }
                }
            }
            catch (Exception e)
            {
                MainControl.Logger.WriteLog(tac.Source.ShowName + " " + tac.AccountInfo.ShowName + " Query error with "
                    + e.ToString(), LoggerFile.LoggerLevel.LoggerError);
                return false;
            }
            return true;
        }

        public override void GetUserInfo(TweetAccount tac)
        {
            List<Parameter> parameters = new List<Parameter>();
            oAuth.OauthKey oauthKey = new oAuth.OauthKey();
            oauthKey.customKey = SourceCfg.customKey;
            oauthKey.customSecrect = SourceCfg.customSecrect;
            oauthKey.tokenKey = tac.AccountCfg.accessKey;
            oauthKey.tokenSecrect = tac.AccountCfg.accessSecrect;

            string queryString, retString;
            try
            {
                HttpRequest request = new HttpRequest();
                oAuth.GetOauthUrl(SourceCfg.requestUrl + ACCOUNT_VERIFY, HttpRequest.HTTP_GET, oauthKey, parameters, out queryString);
                retString = request.HttpGet(SourceCfg.requestUrl + ACCOUNT_VERIFY, queryString);
                if (string.IsNullOrEmpty(retString))
                    return;
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(retString);
                TransferUserNode(doc["user"], tac.AccountInfo);
                /*XmlNodeList nodeList = doc.SelectNodes("/user");
                if (nodeList != null)
                {
                    foreach (XmlNode node in nodeList)
                    {
                        tac.AccountInfo.ID = tac.ID;
                        tac.AccountInfo.ShowName = node["screen_name"].InnerText;
                        tac.AccountInfo.Location = node["location"].InnerText;
                        tac.AccountInfo.HeadUrl = node["profile_image_url"].InnerText;
                        if (string.IsNullOrEmpty(tac.AccountInfo.HeadUrl))
                        {
                            tac.AccountInfo.HeadUrl = "Resource/NoHead.jpg";
                        }
                        tac.AccountInfo.Gender = TransferSex(node["gender"].InnerText);
                        if (node["verified"].InnerText == "true")
                            tac.AccountInfo.VIP = true;
                        else
                            tac.AccountInfo.VIP = false;
                        tac.AccountInfo.Signature = node["description"].InnerText;
                        tac.AccountInfo.friends = Convert.ToInt64(node["friends_count"].InnerText);
                        tac.AccountInfo.follows = Convert.ToInt64(node["followers_count"].InnerText);
                        tac.AccountInfo.topics = Convert.ToInt64(node["statuses_count"].InnerText);
                    }
                }*/
            }
            catch (Exception e)
            {
                MainControl.Logger.WriteLog(tac.Source.ShowName + " " + tac.AccountInfo.ShowName + " GetUserInfo error with "
                    + e.ToString(), LoggerFile.LoggerLevel.LoggerError);
            }
        }

        public override void GetUserInfoByID(TweetAccount tac, UserInfo user)
        {
            List<Parameter> parameters = new List<Parameter>();
            oAuth.OauthKey oauthKey = new oAuth.OauthKey();
            oauthKey.customKey = SourceCfg.customKey;
            oauthKey.customSecrect = SourceCfg.customSecrect;
            oauthKey.tokenKey = tac.AccountCfg.accessKey;
            oauthKey.tokenSecrect = tac.AccountCfg.accessSecrect;

            string queryString, retString;
            try
            {
                HttpRequest request = new HttpRequest();
                string strrequest = SourceCfg.requestUrl + USERS_SHOW + "/" + user.GetID(tac) + REQUEST_TYPE_XML;
                oAuth.GetOauthUrl(strrequest, HttpRequest.HTTP_GET, oauthKey, parameters, out queryString);
                retString = request.HttpGet(strrequest, queryString);
                if (string.IsNullOrEmpty(retString))
                    return;
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(retString);
                TransferUserNode(doc["user"], user);
                /*XmlNodeList nodeList = doc.SelectNodes("/user");
                if (nodeList != null)
                {
                    foreach (XmlNode node in nodeList)
                    {
                        user.ID = node["id"].InnerText;
                        user.ShowName = node["screen_name"].InnerText;
                        user.Location = node["location"].InnerText;
                        user.HeadUrl = node["profile_image_url"].InnerText;
                        if (string.IsNullOrEmpty(user.HeadUrl))
                        {
                            user.HeadUrl = "Resource/NoHead.jpg";
                        }
                        user.Gender = TransferSex(node["gender"].InnerText);
                        if (node["verified"].InnerText == "true")
                            user.VIP = true;
                        else
                            user.VIP = false;
                        user.Signature = node["description"].InnerText;
                        user.friends = Convert.ToInt64(node["friends_count"].InnerText);
                        user.follows = Convert.ToInt64(node["followers_count"].InnerText);
                        user.topics = Convert.ToInt64(node["statuses_count"].InnerText);
                        user.Updated = true;
                    }
                }*/
            }
            catch (Exception e)
            {
                MainControl.Logger.WriteLog(tac.Source.ShowName + " " + tac.AccountInfo.ShowName + " GetUserInfoByID error with "
                    + e.ToString(), LoggerFile.LoggerLevel.LoggerError);
            }
        }

        public override void GetComments(TweetAccount tac, TweetItem ti)
        {
            List<Parameter> parameters = new List<Parameter>();
            oAuth.OauthKey oauthKey = new oAuth.OauthKey();
            oauthKey.customKey = SourceCfg.customKey;
            oauthKey.customSecrect = SourceCfg.customSecrect;
            oauthKey.tokenKey = tac.AccountCfg.accessKey;
            oauthKey.tokenSecrect = tac.AccountCfg.accessSecrect;

            parameters.Add(new Parameter("id", ti.ID));
            //parameters.Add(new Parameter("page", "1"));
            parameters.Add(new Parameter("count", MainControl.GlobalSetting.REFRESH_ITEM_COUNT.ToString()));

            string queryString, retString;
            try
            {
                HttpRequest request = new HttpRequest();
                oAuth.GetOauthUrl(SourceCfg.requestUrl + STATUS_COMMENTS, HttpRequest.HTTP_GET, oauthKey, parameters, out queryString);
                retString = request.HttpGet(SourceCfg.requestUrl + STATUS_COMMENTS, queryString);
                if (string.IsNullOrEmpty(retString))
                    return;
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(retString);
                XmlNodeList nodeList = doc.SelectNodes("/comments/comment");
                if (nodeList != null)
                {
                    foreach (XmlNode node in nodeList)
                    {
                        TweetItem tiComment = new TweetItem(tac, node.InnerXml);
                        UserInfo author = new UserInfo(tac);

                        tiComment.Type = TweetItem.TweetItemType.COMMENT;
                        TransferTweet(node, tiComment);
                        if (node["user"] != null)
                        {
                            TransferUserNode(node["user"], author);
                            MainControl.UpdateAllUser(author, tiComment);
                        }
                        MainControl.UpdateComments(ti, tiComment);
                    }
                }
            }
            catch (Exception e)
            {
                MainControl.Logger.WriteLog(tac.Source.ShowName + " " + tac.AccountInfo.ShowName + "GetComments error with " + ti.ID + " "
                    + e.ToString(), LoggerFile.LoggerLevel.LoggerError);
            }
        }

        public override bool Compose(TweetAccount tac, string content)
        {
            List<Parameter> parameters = new List<Parameter>();
            oAuth.OauthKey oauthKey = new oAuth.OauthKey();
            oauthKey.customKey = SourceCfg.customKey;
            oauthKey.customSecrect = SourceCfg.customSecrect;
            oauthKey.tokenKey = tac.AccountCfg.accessKey;
            oauthKey.tokenSecrect = tac.AccountCfg.accessSecrect;

            UTF8Encoding utf8 = new UTF8Encoding();
            Byte[] encodedBytes = utf8.GetBytes(content);
            parameters.Add(new Parameter("status", utf8.GetString(encodedBytes)));

            string queryString, retString;
            try
            {
                HttpRequest request = new HttpRequest();
                oAuth.GetOauthUrl(SourceCfg.requestUrl + STATUS_UPDATE, HttpRequest.HTTP_POST, oauthKey, parameters, out queryString);
                retString = request.HttpPost(SourceCfg.requestUrl + STATUS_UPDATE, queryString);
                if (string.IsNullOrEmpty(retString))
                    return false;
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(retString);
                if (doc["status"] != null && doc["status"]["id"] != null)
                    return true;
                return false;
            }
            catch (Exception e)
            {
                MainControl.Logger.WriteLog(tac.Source.ShowName + " " + tac.AccountInfo.ShowName + "Compose error with "
                    + e.ToString(), LoggerFile.LoggerLevel.LoggerError);
                return false;
            }
        }

        public override bool Retweet(TweetAccount tac, TweetItem ti, string Content)
        {
            List<Parameter> parameters = new List<Parameter>();
            oAuth.OauthKey oauthKey = new oAuth.OauthKey();
            oauthKey.customKey = SourceCfg.customKey;
            oauthKey.customSecrect = SourceCfg.customSecrect;
            oauthKey.tokenKey = tac.AccountCfg.accessKey;
            oauthKey.tokenSecrect = tac.AccountCfg.accessSecrect;

            parameters.Add(new Parameter("id", ti.ID));
            if (!string.IsNullOrEmpty(Content))
            {
                UTF8Encoding utf8 = new UTF8Encoding();
                Byte[] encodedBytes = utf8.GetBytes(Content);
                parameters.Add(new Parameter("status", utf8.GetString(encodedBytes)));
                parameters.Add(new Parameter("is_comment", "1"));
            }
            else
                parameters.Add(new Parameter("is_comment", "0"));

            string queryString, retString;
            try
            {
                HttpRequest request = new HttpRequest();
                oAuth.GetOauthUrl(SourceCfg.requestUrl + STATUS_RETWEET, HttpRequest.HTTP_POST, oauthKey, parameters, out queryString);
                retString = request.HttpPost(SourceCfg.requestUrl + STATUS_RETWEET, queryString);
                if (string.IsNullOrEmpty(retString))
                    return false;
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(retString);
                if (doc["status"] != null && doc["status"]["id"]!=null)
                    return true;
                return false;
            }
            catch (Exception e)
            {
                MainControl.Logger.WriteLog(tac.Source.ShowName + " " + tac.AccountInfo.ShowName + "Compose error with "
                    + e.ToString(), LoggerFile.LoggerLevel.LoggerError);
                return false;
            }
        }

        public override bool Comment(TweetAccount tac, TweetItem ti, string Content)
        {
            List<Parameter> parameters = new List<Parameter>();
            oAuth.OauthKey oauthKey = new oAuth.OauthKey();
            oauthKey.customKey = SourceCfg.customKey;
            oauthKey.customSecrect = SourceCfg.customSecrect;
            oauthKey.tokenKey = tac.AccountCfg.accessKey;
            oauthKey.tokenSecrect = tac.AccountCfg.accessSecrect;

            parameters.Add(new Parameter("id", ti.ID));
            UTF8Encoding utf8 = new UTF8Encoding();
            Byte[] encodedBytes = utf8.GetBytes(Content);
            parameters.Add(new Parameter("comment", utf8.GetString(encodedBytes)));

            string queryString, retString;
            try
            {
                HttpRequest request = new HttpRequest();
                oAuth.GetOauthUrl(SourceCfg.requestUrl + STATUS_COMMENT, HttpRequest.HTTP_POST, oauthKey, parameters, out queryString);
                retString = request.HttpPost(SourceCfg.requestUrl + STATUS_COMMENT, queryString);
                if (string.IsNullOrEmpty(retString))
                    return false;
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(retString);
                if (doc["comment"] != null && doc["comment"]["id"] != null)
                    return true;
                return false;
            }
            catch (Exception e)
            {
                MainControl.Logger.WriteLog(tac.Source.ShowName + " " + tac.AccountInfo.ShowName + "Compose error with "
                    + e.ToString(), LoggerFile.LoggerLevel.LoggerError);
                return false;
            }
        }

        public override void GetFansList(TweetAccount tac, UserInfo userInfo, UserInfo.UserType uType, int index)
        {
            List<Parameter> parameters = new List<Parameter>();
            oAuth.OauthKey oauthKey = new oAuth.OauthKey();
            oauthKey.customKey = SourceCfg.customKey;
            oauthKey.customSecrect = SourceCfg.customSecrect;
            oauthKey.tokenKey = tac.AccountCfg.accessKey;
            oauthKey.tokenSecrect = tac.AccountCfg.accessSecrect;

            parameters.Add(new Parameter("format", "xml"));
            parameters.Add(new Parameter("count", MainControl.GlobalSetting.REFRESH_ITEM_COUNT.ToString()));
            if (index > 0)
                parameters.Add(new Parameter("cursor", index.ToString()));
            else
                parameters.Add(new Parameter("cursor", "-1"));

            string queryString, retString;
            try
            {
                HttpRequest request = new HttpRequest();
                string requestUrl;
                if (uType == UserInfo.UserType.USER_FANS)
                    requestUrl = SourceCfg.requestUrl + STATUS_FRIENDS;
                else
                    requestUrl = SourceCfg.requestUrl + STATUS_FOLLOWERS;
                if (!string.IsNullOrEmpty(userInfo.GetID(tac)))
                    requestUrl += "/" + userInfo.GetID(tac);

                oAuth.GetOauthUrl(requestUrl + REQUEST_TYPE_XML, HttpRequest.HTTP_GET, oauthKey, parameters, out queryString);
                retString = request.HttpGet(requestUrl + REQUEST_TYPE_XML, queryString);
                if (!string.IsNullOrEmpty(retString))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(retString);
                    if (doc["users"] != null && doc["users"]["next_cursor"] != null)
                    {
                        XmlNodeList nodeList = doc.SelectNodes("/users/user");
                        if (nodeList != null)
                        {
                            foreach (XmlNode node in nodeList)
                            {
                                UserInfo fans = new UserInfo(tac);
                                TransferUserNode(node, fans);
                                MainControl.UpdateFans(userInfo, fans, uType);
                            }
                        }
                        if (doc["users"]["next_cursor"].InnerText != "0")
                        {
                            if (uType == UserInfo.UserType.USER_FANS)
                            {
                                index = Convert.ToInt32(doc["users"]["next_cursor"].InnerText);
                                MainControl.AddQueryFansJob(tac, userInfo, index);
                            }
                            else
                            {
                                index = Convert.ToInt32(doc["users"]["next_cursor"].InnerText);
                                MainControl.AddQueryFriendsJob(tac, userInfo,index);
                            }
                        }
                        else
                        {
                            if (uType == UserInfo.UserType.USER_FANS)
                            {
                                userInfo[tac].FansCompleted = true;
                            }
                            else
                            {
                                userInfo[tac].FriendsCompleted = true;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MainControl.Logger.WriteLog(userInfo.Account.Source.ShowName + " " + userInfo.ShowName + "GetFans error with "
                    + e.ToString(), LoggerFile.LoggerLevel.LoggerError);
            }
        }

        public override bool QueryUserTimeline(TweetAccount tac, UserInfo user)
        {
            List<Parameter> parameters = new List<Parameter>();
            oAuth.OauthKey oauthKey = new oAuth.OauthKey();
            oauthKey.customKey = SourceCfg.customKey;
            oauthKey.customSecrect = SourceCfg.customSecrect;
            oauthKey.tokenKey = tac.AccountCfg.accessKey;
            oauthKey.tokenSecrect = tac.AccountCfg.accessSecrect;

            if (user[tac].LastComposeStamp != 0)
                parameters.Add(new Parameter("since_id", user[tac].LastComposeStamp.ToString()));
            parameters.Add(new Parameter("since_id", "0"));
            parameters.Add(new Parameter("count", MainControl.GlobalSetting.REFRESH_ITEM_COUNT.ToString()));

            string queryString, retString;
            try
            {
                HttpRequest request = new HttpRequest();
                //string strrequest = SourceCfg.requestUrl + STATUS_USER_TIMELINE + REQUEST_TYPE_XML;
                string strrequest = SourceCfg.requestUrl + STATUS_USER_TIMELINE + "/" + user.GetID(tac) + REQUEST_TYPE_XML;
                oAuth.GetOauthUrl(strrequest, HttpRequest.HTTP_GET, oauthKey, parameters, out queryString);
                retString = request.HttpGet(strrequest, queryString);
                if (string.IsNullOrEmpty(retString))
                    return false;
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(retString);
                XmlNodeList nodeList = doc.SelectNodes("/statuses/status");
                if (nodeList != null)
                {
                    foreach (XmlNode node in nodeList)
                    {
                        TweetItem ti = new TweetItem(tac, node.InnerXml);
                        UserInfo author = new UserInfo(tac);
                        ti.Type = TweetItem.TweetItemType.SOURCE;
                        long timestamp = TransferTweet(node, ti);
                        if (timestamp > user[tac].LastComposeStamp)
                            user[tac].LastComposeStamp = timestamp;

                        if (node["user"] != null)
                        {
                            TransferUserNode(node["user"], author);
                            MainControl.UpdateAllUser(author, ti);
                        }
                        if (node["retweeted_status"] != null)
                        {
                            ti.SourceItem = new TweetItem(tac, node["retweeted_status"].InnerXml);
                            TransferTweet(node["retweeted_status"], ti.SourceItem);

                            if (node["retweeted_status"]["user"] != null)
                            {
                                UserInfo oriAuthor = new UserInfo(tac);
                                TransferUserNode(node["retweeted_status"]["user"], oriAuthor);
                                MainControl.UpdateAllUser(oriAuthor, ti.SourceItem);
                            }
                        }
                        MainControl.UpdateUserTimeline(user, ti);
                    }
                }
            }
            catch (Exception e)
            {
                MainControl.Logger.WriteLog(tac.Source.ShowName + " " + tac.AccountInfo.ShowName + " Query user time line error with "
                    + e.ToString(), LoggerFile.LoggerLevel.LoggerError);
                return false;
            }
            return true;
        }

        public override bool DeleteTweet(TweetAccount tac, TweetItem ti)
        {
            List<Parameter> parameters = new List<Parameter>();
            oAuth.OauthKey oauthKey = new oAuth.OauthKey();
            oauthKey.customKey = SourceCfg.customKey;
            oauthKey.customSecrect = SourceCfg.customSecrect;
            oauthKey.tokenKey = tac.AccountCfg.accessKey;
            oauthKey.tokenSecrect = tac.AccountCfg.accessSecrect;

            string queryString, retString;
            try
            {
                HttpRequest request = new HttpRequest();
                //string strrequest = SourceCfg.requestUrl + STATUS_DESTORY + REQUEST_TYPE_XML;
                string strrequest = SourceCfg.requestUrl + STATUS_DESTROY + "/" + ti.ID + REQUEST_TYPE_XML;
                oAuth.GetOauthUrl(strrequest, HttpRequest.HTTP_POST, oauthKey, parameters, out queryString);
                retString = request.HttpPost(strrequest, queryString);
                if (string.IsNullOrEmpty(retString))
                    return false;
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(retString);
                if (doc["status"] != null && doc["status"]["id"] != null)
                {
                    return true;
                }
                return false;
            }
            catch (Exception e)
            {
                MainControl.Logger.WriteLog(tac.Source.ShowName + " " + tac.AccountInfo.ShowName + " Delete tweet error with "
                    + ti.ID.ToString() + e.ToString(), LoggerFile.LoggerLevel.LoggerError);
                return false;
            }
        }
    }

}
